import java.util.Arrays;

/**
 * @author JiuQi 玖柒
 * @date 2021/7/6 22:47
 * 旋转数组
 */
public class T189 {
    public static void main(String[] args) {
        int[] nums = {-1, -100, 3, 99};
        int k = 2;
        new Solution4().rotate(nums, k);
    }
}

/*class Solution1 {
    public void rotate(int[] nums, int k) {
        for (int i = 0; i < k; i++) {
            int temp = 0;
            temp = nums[nums.length - 1];
            for (int j = nums.length - 1; j >= 1; j--) {
                nums[j] = nums[j - 1];
            }
            nums[0] = temp;
        }
        System.out.println(Arrays.toString(nums));
    }
}*/
class Solution4 {
    public void rotate(int[] nums, int k) {
        int n = nums.length;
        k %= n;
        if (k == 0) {
            return;
        }
        //全局翻转
        reverse(nums, 0, n - 1);
        //左边翻转
        reverse(nums, 0, k - 1);
        //右边翻转
        reverse(nums, k, n - 1);
        System.out.println(Arrays.toString(nums));
    }

    private void reverse(int[] nums, int left, int right) {
        while (left < right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
    }
}
